package com.share.cloudStorage.advice;

import com.share.cloudStorage.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: javaKing
 * @Date: 2024/12/09/16:04
 * @Description:
 */
@RestControllerAdvice
@Slf4j
public class RestResponseBodyAdvice implements ResponseBodyAdvice {

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        log.debug("returnType:{}", returnType);
        log.debug("converterType:{}", converterType);
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        log.debug("body:{}", body);
        log.debug("selectedContentType:{}", selectedContentType);
        log.debug("selectedConverterType:{}", selectedConverterType);
        String path = request.getURI().getPath();
        if (path.contains("/v3/api-docs")){
            return body;
        }
        if (path.contains("v1/files/downLoadFile")){
            return body;
        }

        if (!(body instanceof ResponseEntity) && !(body instanceof Result)) {
            return Result.ok(body);
        }
        return body;
    }
}
